package org.cid54.android.jshopper.dao.impl;

import android.os.AsyncTask;
import java.util.ArrayList;
import java.util.List;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.UserGroupDao;
import org.cid54.android.jshopper.data.CsvFetcher;
import org.cid54.android.jshopper.data.impl.CsvFetcherImpl;

/**
 *
 * @author fgo
 */
public class UserGroupDaoImpl implements UserGroupDao {

    private final String userGroupsSubUrl = "usergroups";
    final CsvFetcher fetcher = new CsvFetcherImpl();

    public void getUserGroups(final DaoCallBackMethod<List<String>> userGroups) {

        AsyncTask<Void, Void, List<String>> task = new AsyncTask<Void, Void, List<String>>() {

            @Override
            protected List<String> doInBackground(Void... arg0) {

                List<String> result = new ArrayList<String>();

                for (String[] line : fetcher.getData(userGroupsSubUrl)) {

                    result.add(line[0]);

                }

                return result;

            }

            @Override
            protected void onPostExecute(List<String> result) {
                super.onPostExecute(result);
                userGroups.execute(result);
            }
        };

        task.execute();

    }
}
